# -*- coding: utf-8 -*

import random
import pandas
import numpy
import sys



class hiron():
    def __init__(self,num,lab):
        self.pm = 0.6
        self.pc = 0.01
        self.population = self.origon()
        self.results = []
        self.individual = []


        for z in range(2):
            print(z)

            fuction = self.fuction(self.population, num)
            part_individual,max_finess=self.best(fuction)
            self.results.append(max_finess)
            self.individual.append(part_individual)
            print(max_finess)
            for r in range(len(fuction)):
                fuction[r] = 1/fuction[r]
            self.selection(fuction,part_individual)
            self.crossover()
        #self.out = self.best1()


    def origon(self):
        self.population = numpy.zeros((400, 32))
        i = 0
        while i < 400:
            temporary = random.sample(range(1, 33), 32)
            self.population[i, :] = temporary
            i = i + 1
        return self.population

    def fuction(self, popula,num):
        population=popula.copy()
        #population[0]=[3.0, 6.0, 5.0, 8.0, 11.0, 7.0, 13.0, 9.0, 12.0, 10.0, 1.0, 14.0, 18.0, 24.0, 20.0, 22.0, 17.0, 19.0, 23.0, 16.0, 15.0, 21.0, 2.0, 27.0, 25.0, 26.0, 4.0, 30.0, 32.0, 28.0, 31.0, 29.0]
            #[6,27,6,14,27,6,14,14,14,14,14,24,14,14,24,24,21,21,21,21,21,21,21,27,6,32,32,32,32,32,1,1]
        total = 0
        sort = []
        for i in range(len(num)):
            total += num[i]
            sort.append(total)
        dict = {}  # 1-32  分别对应的种类
        for i in range(1,33):
            for n in range(len(sort)):
                if i <= sort[0]:
                    dict[i] = 1
                    break
                else:
                    if i > sort[n - 1] and i <= sort[n]:
                        dict[i] = n + 1
                        break
        for i in range(len(population)):
            for j in range(len(population[i])):
                population[i][j] = dict[population[i][j]]
        fuction = []
        Time = pandas.read_excel("D:\web_develop\idea\work\ERP\MES_parent\MES_service_provider_base\src\main\resources\dog_data.xlsx")
        print(1)
        T1=numpy.array(Time)[:,1:]
        print(1)
        for n in range(400):
            start = numpy.zeros((32, 21))
            for i in range(1, 32):#第一个工作站开始时间
                start[i,0]=35*i#节拍
                start[i, 0] = max(start[i, 0], start[i - 1, 0] + T1[int(population[n][i - 1] - 1), 0])
            for j in range(1,21):#第一个产品所有工作站开始时间
                start[0,j]=start[0,j-1]+T1[int(population[n][0] - 1), j-1]
            for i in range(1,32):
                for j in range(1,21):
                    start[i,j]=max(start[i,j-1]+T1[int(population[n][i] - 1), j-1],start[i-1,j]+T1[int(population[n][i-1] - 1), j])
            fuction.append(start[31,20])
        print(1)
        return fuction

    def selection(self, fuction,part_individual):
        ss = sum(fuction)
        for f in range(len(fuction)):
            fuction[f] = fuction[f] / ss
        self.population = self.population[
            numpy.random.choice(numpy.arange(len(self.population)), size=392, replace=True,
                                p=fuction)]
        for i in range(8):
            self.population = numpy.insert(self.population, random.randint(0, len(self.population) - 1),
                                           part_individual, 0)

    def crossover(self):
        for i in range(len(self.population)):
            if (random.random()) < 0.6:
                a = random.randint(0, 30)
                b = random.randint(a + 1, 31)

                while True:
                    k = random.randint(0, len(self.population) - 1)
                    if k != i:
                        break


                a1 = self.population[i,a:b+1]
                b1 = self.population[k,a:b+1]
                #for j in range(a, b + 1):
                    #a1.append(self.population[i][j])
                    #b1.append(self.population[k][j])
                a1=a1.copy()
                b1=b1.copy()

                dict = {}
                for j in range(len(a1)):
                    for jj in range(len(b1)):
                        if a1[j]==b1[jj]:
                            t=b1[j]
                            b1[j]=b1[jj]
                            b1[jj]=t
                for j in range(len(a1)):
                    if a1[j]!=b1[j]:
                        dict[a1[j]]=b1[j]

                self.population[i][a:b + 1] = b1
                for j in range(a):
                    for key in dict:
                        if self.population[i][j]==key:
                            self.population[i][j] = dict[key]
                            break
                        if self.population[i][j] == dict[key]:
                            self.population[i][j]=key
                            break
                for j in range(b+1,len(self.population[i])):
                    for key in dict:
                        if self.population[i][j]==key:
                            self.population[i][j] = dict[key]
                            break
                        if self.population[i][j] == dict[key]:
                            self.population[i][j]=key
                            break

                self.population[k][a:b + 1] = a1
                for j in range(a):
                    for key in dict:
                        if self.population[k][j]==key:
                            self.population[k][j] = dict[key]
                            break
                        if self.population[k][j] == dict[key]:
                            self.population[k][j]=key
                            break
                for j in range(b+1,len(self.population[i])):
                    for key in dict:
                        if self.population[k][j]==key:
                            self.population[k][j] = dict[key]
                            break
                        if self.population[k][j] == dict[key]:
                            self.population[k][j]=key
                            break

    def best(self, fuction):
        px = len(self.population)

        findividual = self.population[0].tolist()
        ffitness = fuction[0]
        for i in range(1, px):
            # 循环找出最大的适应度，适应度最大的也就是最好的个体
            if fuction[i] < ffitness:
                findividual = self.population[i].tolist()
                ffitness=fuction[i]

        return findividual,ffitness
    def best1(self):
        px = len(self.individual)

        findividual = self.individual[0]

        ffitness = self.results[0]
        for i in range(1, px):
            # 循环找出最大的适应度，适应度最大的也就是最好的个体
            if self.results[i] < ffitness:
                findividual = self.individual[i]
                ffitness = self.results[i]
        num=[8,5,10,3,3,3]
        total = 0
        sort = []
        for i in range(len(num)):
            total += num[i]
            sort.append(total)
        dict = {}  # 1-32  分别对应的种类
        for i in range(1,33):
            for n in range(len(sort)):
                if i <= sort[0]:
                    dict[i] = 1
                    break
                else:
                    if i > sort[n - 1] and i <= sort[n]:
                        dict[i] = n + 1
                        break

        for i in range(len(findividual)):
            findividual[i] = dict[int(findividual[i])]

        # print(findividual)
        # print(ffitness)
        print( findividual)

def start(num,lab):
    hir = hiron(num,lab)


if __name__ == "__main__":
    #print(start(sys.argv[1],sys.argv[2]))
    start([8,5,10,3,3,3],[8,5,10,3,3,3])